package jdk8;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class StreamTest01 {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(3, 2, 12, 5, 6, 11, 13);
        long count = list.stream()
                .filter(i -> i % 2 == 0)
                .count();
        System.out.println("count = " + count);
        doTest02();
        map();
        match();
        find();
    }

    static void doTest02() {
        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        List<Integer> collect = integers.stream()
                .filter((integer) -> integer % 2 == 0)
                .collect(Collectors.toList());
        System.out.println("collect = " + collect);
    }

    /**
     * 映射操作(map)
     */
    static void map(){
        List<String> list=Arrays.asList("a","bc","def");
        list.stream()
                .map((x)->x.toUpperCase()) //将集合中元素转换为大写
                .forEach(System.out::println);
    }

    static void match(){
        List<Integer> list=Arrays.asList(10,11,12,13,14,15);
        boolean flag = list.stream().allMatch(x -> x % 2 == 0);
        System.out.println(flag);
        flag=list.stream().anyMatch((x)->x%2==0);//是否存在匹配
        System.out.println(flag);
        flag=list.stream().noneMatch((x)->x>20);//是否没有匹配项
        System.out.println(flag);
    }

    static void find(){
        List<Integer> list=Arrays.asList(10,11,12,13,14,15);
        Optional<Integer> optional = list.stream().sorted().findFirst();
        System.out.println(optional.get());
        optional=list.parallelStream().filter((x)->x%2!=0).findAny();
        System.out.println(optional.get());
    }
}
